﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class FieldSpecDetailDAL : IFieldSpecDetailDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public FieldSpecDetailDAL()
        {

        }
        public IList<FieldSpecDetailInfo> ListFieldSpecDetails(FieldSpecDetailInfo detailInfo)
        {
            string strSQL;
            int specSeq = detailInfo.FieldSpec == null ? 0 : detailInfo.FieldSpec.Sequence;

            List<FieldSpecDetailInfo> infoList = new List<FieldSpecDetailInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Sequence", detailInfo.Sequence);
                dbManager.AddParameters(2, "@specSeq", specSeq);

                strSQL = "SELECT FIELD_SPEC_DTLS_SEQ, FIELD_SPEC_SEQ, POS_TYPE_SEQ, CLASS_SEQ, DURATIONS"
                         + ", REMARKS, RECORD_STATUS, CREATED_BY, UPDATED_BY"
                     + " FROM HRM_MP_FIELD_SPECIFICATION_DTLS"
                     + " WHERE RECORD_STATUS not like @DeleteRecord"
                     + (detailInfo.Sequence > 0 ? " AND FIELD_SPEC_SEQ = @Sequence" : "")
                     + (specSeq > 0 ? " AND FIELD_SPEC_SEQ = @specSeq" : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    FieldSpecificationDAL specDAL = new FieldSpecificationDAL();
                    FieldSpecificationInfo specInfo = new FieldSpecificationInfo();
                    if (!rdr.IsDBNull(1))
                    {
                        IList<FieldSpecificationInfo> specList = specDAL.ListFieldSpecification(new FieldSpecificationInfo(rdr.GetInt32(1)));
                        specInfo = specList[0];
                    }

                    PositionTypeDAL posDAL = new PositionTypeDAL();
                    PositionTypeInfo postypeInfo = new PositionTypeInfo();
                    if (!rdr.IsDBNull(2))
                    {
                        IList<PositionTypeInfo> posList = posDAL.ListPositionTypes(rdr.IsDBNull(2) ? (int?)null : rdr.GetInt32(2), null);
                        postypeInfo = posList[0];
                    }

                    ClassDAL classDAL = new ClassDAL();
                    ClassInfo classInfo = new ClassInfo();
                    if (!rdr.IsDBNull(3))
                    {
                        IList<ClassInfo> classList = classDAL.ListClasses(new ClassInfo(rdr.GetInt32(3)));
                        classInfo = classList[0];
                    }

                    FieldSpecDetailInfo Info = new FieldSpecDetailInfo(rdr.GetInt32(0)
                            , specInfo, postypeInfo, classInfo
                            , rdr.IsDBNull(4) ? 0 : rdr.GetDouble(4)
                            , rdr.IsDBNull(5) ? "" : rdr.GetString(5)
                            , rdr.IsDBNull(6) ? "" : rdr.GetString(6)
                            , rdr.IsDBNull(7) ? (int?)null : rdr.GetInt32(7)
                            , rdr.IsDBNull(8) ? (int?)null : rdr.GetInt32(8));
                    infoList.Add(Info);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return infoList;
        }

        public void Insert(FieldSpecDetailInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@FieldSpecSeq", Info.FieldSpec.Sequence);
                dbManager.AddParameters(1, "@PosTypeSeq", Info.PositionTypes.Sequence);
                dbManager.AddParameters(2, "@ClassSeq", Info.Classes.Sequence);
                dbManager.AddParameters(3, "@Duration", Info.Durations);
                dbManager.AddParameters(4, "@Remarks", Info.Remarks);
                dbManager.AddParameters(5, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(6, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(7, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_MP_FIELD_SPECIFICATION_DTLS("
                        + " FIELD_SPEC_SEQ, POS_TYPE_SEQ, CLASS_SEQ, DURATIONS"
                        + ", REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@FieldSpecSeq, @PosTypeSeq, @ClassSeq, @Duration"
                        + ", @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(FieldSpecDetailInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@Sequence", Info.Sequence);
                dbManager.AddParameters(1, "@FieldSpecSeq", Info.FieldSpec.Sequence);
                dbManager.AddParameters(2, "@PosTypeSeq", Info.PositionTypes.Sequence);
                dbManager.AddParameters(3, "@ClassSeq", Info.Classes.Sequence);
                dbManager.AddParameters(4, "@Duration", Info.Durations);
                dbManager.AddParameters(5, "@Remarks", Info.Remarks);
                dbManager.AddParameters(6, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(7, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_MP_FIELD_SPECIFICATION_DTLS"
                           + " SET FIELD_SPEC_SEQ = @FieldSpecSeq"
                                + ", POS_TYPE_SEQ = @PosTypeSeq"
                                + ", CLASS_SEQ = @ClassSeq"
                                + ", DURATIONS = @Duration"
                                + ", REMARKS = @Remarks"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE FIELD_SPEC_DTLS_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_MP_FIELD_SPECIFICATION_DTLS"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE FIELD_SPEC_DTLS_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
